package com.eshop.tools.rest;

import org.slf4j.LoggerFactory;
import java.util.Map;
import com.eshop.tools.utils.AliPayStatusEnum;
import com.eshop.annotation.AnonymousAccess;
import springfox.documentation.annotations.ApiIgnore;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.tools.domain.vo.TradeVo;
import org.springframework.web.bind.annotation.PutMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.HttpStatus;
import com.eshop.tools.domain.AlipayConfig;
import org.springframework.http.ResponseEntity;
import com.eshop.tools.service.AlipayConfigService;
import com.eshop.tools.utils.AlipayUtils;
import org.slf4j.Logger;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/aliPay" })
@Api(tags = { "工具：支付宝管理" })
public class AliPayController
{
    private static final Logger log;
    private final AlipayUtils alipayUtils;
    private final AlipayConfigService alipayService;
    
    public AliPayController(final AlipayUtils alipayUtils, final AlipayConfigService alipayService) {
        this.alipayUtils = alipayUtils;
        this.alipayService = alipayService;
    }
    
    @GetMapping
    public ResponseEntity<AlipayConfig> get() {
        return (ResponseEntity<AlipayConfig>)new ResponseEntity(this.alipayService.find(), HttpStatus.OK);
    }
    
    @Log("配置支付宝")
    @ApiOperation("配置支付宝")
    @PutMapping
    public ResponseEntity<Object> payConfig(@Validated @RequestBody final AlipayConfig alipayConfig) {
        alipayConfig.setId(1L);
        this.alipayService.update(alipayConfig);
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("支付宝PC网页支付")
    @ApiOperation("PC网页支付")
    @PostMapping({ "/toPayAsPC" })
    public ResponseEntity<String> toPayAsPc(@Validated @RequestBody final TradeVo trade) throws Exception {
        final AlipayConfig aliPay = this.alipayService.find();
        trade.setOutTradeNo(this.alipayUtils.getOrderCode());
        final String payUrl = this.alipayService.toPayAsPc(aliPay, trade);
        return ResponseEntity.ok(payUrl);
    }
    
    @Log("支付宝手机网页支付")
    @ApiOperation("手机网页支付")
    @PostMapping({ "/toPayAsWeb" })
    public ResponseEntity<String> toPayAsWeb(@Validated @RequestBody final TradeVo trade) throws Exception {
        final AlipayConfig alipay = this.alipayService.find();
        trade.setOutTradeNo(this.alipayUtils.getOrderCode());
        final String payUrl = this.alipayService.toPayAsWeb(alipay, trade);
        return ResponseEntity.ok(payUrl);
    }
    
    @ApiIgnore
    @GetMapping({ "/return" })
    @AnonymousAccess
    @ApiOperation("支付之后跳转的链接")
    public ResponseEntity<String> returnPage(final HttpServletRequest request, final HttpServletResponse response) {
        final AlipayConfig alipay = this.alipayService.find();
        response.setContentType("text/html;charset=" + alipay.getCharset());
        if (this.alipayUtils.rsaCheck(request, alipay)) {
            final String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            final String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            System.out.println("商户订单号" + outTradeNo + "  第三方交易号" + tradeNo);
            return new ResponseEntity("payment successful", HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
    
    @ApiIgnore
    @RequestMapping({ "/notify" })
    @AnonymousAccess
    @ApiOperation("支付异步通知(要公网访问)，接收异步通知，检查通知内容app_id、out_trade_no、total_amount是否与请求中的一致，根据trade_status进行后续业务处理")
    public ResponseEntity<Object> notify(final HttpServletRequest request) {
        final AlipayConfig alipay = this.alipayService.find();
        final Map<String, String[]> parameterMap = (Map<String, String[]>)request.getParameterMap();
        if (this.alipayUtils.rsaCheck(request, alipay)) {
            final String tradeStatus = new String(request.getParameter("trade_status").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            final String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            final String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            final String totalAmount = new String(request.getParameter("total_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            if (tradeStatus.equals(AliPayStatusEnum.SUCCESS.getValue()) || tradeStatus.equals(AliPayStatusEnum.FINISHED.getValue())) {}
            return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
        }
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)AliPayController.class);
    }
}
